package com.droowa.util;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import com.droowa.domain.CompletePCResult;

@Component
public class CompletePCView  extends AbstractExcelView{

	@SuppressWarnings("unchecked")
	@Override
	protected void buildExcelDocument(Map<String, Object> model,
			HSSFWorkbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		response.setHeader("Content-Disposition", "attachment; filename=completePC.xls");
		HSSFSheet sheet = createFirstSheet(workbook);
		createColumnLabel(sheet);
		
		List<CompletePCResult> completePCResult = (List<CompletePCResult>) model.get("completePC");
		int rowNum=1;
		for(CompletePCResult com : completePCResult){
			createProductRow(sheet, com, rowNum++);
		}
	}

	private HSSFSheet createFirstSheet(HSSFWorkbook workbook) {
		HSSFSheet sheet = workbook.createSheet();
		workbook.setSheetName(0, "상품정보");//0번 sheet에 업무기록이라는 이름 부여
		sheet.setColumnWidth((short)1, (short)(8000));
		sheet.setColumnWidth((short)2, (short)(2500));
		sheet.setColumnWidth((short)3, (short)(2500));
		sheet.setColumnWidth((short)4, (short)(6000));
		sheet.setColumnWidth((short)5, (short)(6000));
		sheet.setColumnWidth((short)6, (short)(6000));
		sheet.setColumnWidth((short)7, (short)(6000));
		sheet.setColumnWidth((short)8, (short)(6000));
		sheet.setColumnWidth((short)9, (short)(6000));
		sheet.setColumnWidth((short)10, (short)(6000));
		sheet.setColumnWidth((short)11, (short)(6000));
		sheet.setColumnWidth((short)12, (short)(6000));
		sheet.setColumnWidth((short)13, (short)(6000));
		sheet.setColumnWidth((short)14, (short)(6000));
		sheet.setColumnWidth((short)15, (short)(6000));
		return sheet;
	}
	
	private void createColumnLabel(HSSFSheet sheet) {
		HSSFRow firstRow = sheet.createRow(0);
		HSSFCell cell = firstRow.createCell((short) 0);
		cell.setCellValue("ID");
		
		cell = firstRow.createCell((short) 1);
		cell.setCellValue("이름");
		
		cell = firstRow.createCell((short) 2);
		cell.setCellValue("가격");		
		
		cell = firstRow.createCell((short) 3);
		cell.setCellValue("재고");		
		
		cell = firstRow.createCell((short) 4);
		cell.setCellValue("CPU");
		
		cell = firstRow.createCell((short) 5);
		cell.setCellValue("Mainboard");

		cell = firstRow.createCell((short) 6);
		cell.setCellValue("Cooler");
		
		cell = firstRow.createCell((short) 7);
		cell.setCellValue("Memory");
		
		cell = firstRow.createCell((short) 8);
		cell.setCellValue("Graphic");
		
		cell = firstRow.createCell((short) 9);
		cell.setCellValue("HDD");
		
		cell = firstRow.createCell((short) 10);
		cell.setCellValue("SSD");
		
		cell = firstRow.createCell((short) 11);
		cell.setCellValue("DVD");
		
		cell = firstRow.createCell((short) 12);
		cell.setCellValue("Sound");
		
		cell = firstRow.createCell((short) 13);
		cell.setCellValue("Power");
		
		cell = firstRow.createCell((short) 14);
		cell.setCellValue("Case");
		
		cell = firstRow.createCell((short) 15);
		cell.setCellValue("OS");
		
	}
	
	private void createProductRow(HSSFSheet sheet, CompletePCResult completePCResult, int rowNum) {
		HSSFRow row = sheet.createRow(rowNum);
		HSSFCell cell = row.createCell((short)0);
		cell.setCellValue(completePCResult.getCompleteId());
		
		cell = row.createCell((short)1);
		cell.setCellValue(completePCResult.getCompleteName());
		
		cell = row.createCell((short)2);
		cell.setCellValue(completePCResult.getCompletePrice());
		
		cell = row.createCell((short)3);
		cell.setCellValue(completePCResult.getCompleteQuantity());
		
		cell = row.createCell((short)4);
		cell.setCellValue(completePCResult.getCpuName());
		
		cell = row.createCell((short)5);
		cell.setCellValue(completePCResult.getMainboardName());
		
		cell = row.createCell((short)6);
		cell.setCellValue(completePCResult.getCoolerName());
		
		cell = row.createCell((short)7);
		cell.setCellValue(completePCResult.getMemoryName());

		cell = row.createCell((short)8);
		cell.setCellValue(completePCResult.getGraphicName());

		cell = row.createCell((short)9);
		cell.setCellValue(completePCResult.getHddName());

		cell = row.createCell((short)10);
		cell.setCellValue(completePCResult.getSsdName());

		cell = row.createCell((short)11);
		cell.setCellValue(completePCResult.getDvdName());

		cell = row.createCell((short)12);
		cell.setCellValue(completePCResult.getSoundName());

		cell = row.createCell((short)13);
		cell.setCellValue(completePCResult.getPowerName());

		cell = row.createCell((short)14);
		cell.setCellValue(completePCResult.getCaseName());
		
		cell = row.createCell((short)15);
		cell.setCellValue(completePCResult.getOsName());
		
	}
	
	
}
